package com.fourforfo.fourmall.ware.dao;

import com.fourforfo.common.to.SkuStockTO;
import com.fourforfo.common.to.mq.StockDetailTO;
import com.fourforfo.fourmall.ware.entity.WareOrderTaskDetailEntity;
import com.fourforfo.fourmall.ware.entity.WareSkuEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

/**
 * 商品库存
 * 
 * @author fourforfo
 * @email fourforfo@gmail.com
 * @date 2022-06-16 12:41:34
 */
@Mapper
public interface WareSkuDao extends BaseMapper<WareSkuEntity> {
    /**
     * 批量更新sku_id和ware_id对应的wms_ware_sku表中的stock数量就行
     * @param wareSkuEntities
     */
    void addStock(@Param("entities") List<WareSkuEntity> wareSkuEntities);

    /**
     * 批量插入wareSkuEntity
     * @param wareSkuEntities
     */
    void insertBatch(@Param("entities") List<WareSkuEntity> wareSkuEntities);

    /**
     * SELECT sku_id,SUM(stock-stock_locked) AS stock from `wms_ware_sku` where sku_id in(?,?...) GROUP BY sku_id
     * @param skuIds
     * @return
     */
    List<SkuStockTO> listBySkus(@Param("skuIds") List<Long> skuIds);

    /**
     * 尝试锁定特定仓库的库存数量
     * @param skuId 锁定的商品项
     * @param num 锁定数量
     * @param wareId 仓库id
     * @return 影响的行数
     */
    Integer lockSkuStock(@Param("skuId") Long skuId,@Param("num") Integer num,@Param("wareId") Long wareId);

    /**
     * 查询有库存大于锁定属性的仓库id
     * @param skuId 商品id
     * @param count 商品待锁定的数量
     * @return
     */
    List<Long> listWareIdBySkuIdsHasStock(@Param("skuId") Long skuId, @Param("count") Integer count);

    /**
     * 根据库存详情信息解锁这些商品的库存
     * @param details
     */
    void unLockStocks(@Param("details") List<WareOrderTaskDetailEntity> details);
}
